/*This file is part of WZLibCP.

 *  WZLibCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  WZLibCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with WZLibCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#ifndef __WZLIB_WZFILE_H
#define __WZLIB_WZFILE_H
#include "WZLibConfig.h"

#include <string>

#include "WZObject.h"
#include "WZBinaryReader.h"

namespace WZLib{
	class WZFileHeader;
	class WZDirectory;
	/**\brief Class to represent a file in WZLibCP.

	WZFile represents a .wz file.  It contains a file header for this specific file,
	a root WZDirectory, a WZBinaryReader to read from this file, among some other things.
	*/
	class WZFile:public WZObject{
	protected:
		//!A WZFileHeader containing header info for this file.
		WZFileHeader*		_header;
		//!The root WZDirectory that holds the WZImage s and WZDirectory s contained in the file.
		WZDirectory*		_wzDir;
		//!A WZBinaryReader to read from the file.
		WZBinaryReader*		_reader;
		//!The path to the file.
		std::string			_path;
		//!Encoded game version associated with this file.
		short				_version;
		//!Hash of the game version.
		unsigned int		_versionHash;
		//!Unencoded game version associated with this file.
		short				_fileVersion;
		//Methods
		//!Private function to hash a version number and determine if it's the correct one.
		unsigned int		getVersionHash(int encver,int realver);
		//Constructor
		/**\brief Constructor for WZFile.

		Don't use this function.  Use WZFile::Open instead.

		\param filePath Path to WZFile.
		*/
		ctor				WZFile(std::string filePath);
	public:
		/**\brief WZFile creator.

		If this returns NULL, then the file cannot be found.

		\param filePath Path to WZFile.
		\param searchSearchDirectory Whether or not to search in WZLib::WZConfig::wzSearchDir.
		*/
		static WZFile*		Open(std::string filePath,bool searchSearchDirectory=true);
		//Destructor
		/**\brief Destructor for WZFile.

		Deletes _header,_wzDir, closes the WZBinaryReader and subsequently deletes it.
		*/
		dtor				~WZFile();
		/**\brief Unparser for WZFile.
		
		Does the same things as ~WZFile without the side effect of the pointer becoming invalid.
		*/
		void				unparse();
		//Getters/Setters
		//!Get the root WZDirectory of this file.
		WZDirectory*		wzDir();
		//!Get the game version determined by parse (assuming it has already been called).
		short				fileVersion();
		//Methods
		/**\brief Parse the WZFile.

		As can be seen by the declaration of this function, there is no need to supply a
		WZBinaryReader as WZFile automatically opens one in the constructor and stores it
		in _reader.

		\param reader Unneeded (keep it NULL).
		*/
		virtual ErrorCode	parse(WZBinaryReader* reader=NULL);
	};
};

#endif